<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            height: 516px;
            width: 368px;
            border: 2px solid red;
            margin: 0 auto;
            overflow: hidden;
            position: relative;
        }
        .box .rush{
            width: 1104px;
            height: 526px;
            position: absolute;
            top: 0px;
            left: 0px;
        }
        .box .rush>div{
            float: left;
        }
        .box .left{
            position: absolute;
            left: 0px;
            top: 0px;
            bottom: 0px;
            margin: auto 0px;
            height: 60px;
            width: 30px;
            background-color: red;
            cursor: pointer;
            display: none;
        }
        .box .right{
            position: absolute;
            right: 0px;
            top: 0px;
            bottom: 0px;
            margin: auto 0px;
            height: 60px;
            width: 30px;
            background-color: red;
            cursor: pointer;
            display: none;
        }
        .box ul{
            padding: 0px;
            margin: 0px;
            position: absolute;
            bottom: 0px;
            left: 0px;
            right: 0px;
            width: 100px;
            margin: 0 auto;
            height: 15px;
        }
        .box ul li{
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background-color: white;
            float: left;
            margin-left:10px ;
            margin-right:10px ;
            list-style-type: none;
            cursor: pointer;
            transition: all .1s;
        }

    </style>
</head>
<body>
    <div class="box">
        <div class="rush">
            <div class="p3">
                <img src="./img/黎泽洋 2.png" alt="">
            </div>
            <div class="p1">
                <img src="./img/郭纪要.png" alt="">
            </div>
            <div class="p2">
                <img src="./img/郭钦宇.png" alt="">
            </div>
            <div class="p4">
                <img src="./img/陈旭.png" alt="">
            </div>
            <div class="p3">
                <img src="./img/黎泽洋 2.png" alt="">
            </div>
            <div class="p1">
                <img src="./img/郭纪要.png" alt="">
            </div>
        </div>
        <div class="left"></div>
        <div class="right"></div>
        <ul>

        </ul>
    </div>
    <script src="./js/运动.js"></script>
    <script>
        var left = document.querySelector('.left');
        var ul = document.querySelector('ul');
        var box = document.querySelector('.box');
        var right = document.querySelector('.right');
        var rush = document.querySelector('.rush');
        var num = document.querySelector('.rush').querySelectorAll('div');
        var cnt = 1;
        var focusWidth = num[0].offsetWidth;
        rush.style.width = focusWidth * num.length + 'px';
        rush.style.left = -focusWidth + 'px';
        ul.style.width = 30 * (num.length - 2) + 'px';
        box.addEventListener('mouseover',function(){
            right.style.display = 'block';
            left.style.display = 'block';
            clearInterval(timer);
            timer = null;
        })
        box.addEventListener('mouseleave',function(){
            right.style.display = 'none';
            left.style.display = 'none';
            timer = setInterval( function(){
                right.click();
            },2000);
        })
        for( var i = 0 ; i < num.length - 2 ; i++){
            var li = document.createElement('li');
            li.setAttribute('data-index',i);
            ul.appendChild(li);
            li.addEventListener('click',function(){
                for (var i = 0; i < num.length - 2 ; i++){
                    ul.children[i].style.backgroundColor = 'white';
                    this.style.backgroundColor = 'red';
                    var index = this.getAttribute('data-index');
                    animate(rush, -focusWidth * index - focusWidth);
                    cnt = index*1 + 1;
                }
            })
        }
        ul.children[0].style.backgroundColor = 'red';
        var flag = true;
        right.addEventListener('click',function(){
            if(flag){
                flag = false;
                cnt++;
                if(cnt == num.length - 1){
                    cnt = 1;
                    rush.style.left = 0 + 'px';
                }
                animate(rush, -focusWidth * cnt);
                for (var i = 0; i < num.length - 2 ; i++){
                    if(i == cnt - 1){
                        ul.children[i].style.backgroundColor = 'red';
                    }else{
                        ul.children[i].style.backgroundColor = 'white';
                    }
                }
            }
        })
        left.addEventListener('click',function(){
            if(flag){
                flag = false;
                cnt--;
                if(cnt == -1){
                    cnt = num.length - 3 ;
                    rush.style.left = -focusWidth * (num.length - 2) + 'px';
                }
                animate(rush, -focusWidth * cnt);
                if(cnt == 0){
                    ul.lastChild.style.backgroundColor = 'red';
                    ul.children[0].style.backgroundColor = 'white';

                }else{
                    for (var i = 0; i < num.length - 2 ; i++){ 
                        if(i == cnt - 1 ){
                            ul.children[i].style.backgroundColor = 'red';
                        }else{
                            ul.children[i].style.backgroundColor = 'white';
                        }
                    }
                }
            }
            })
        var timer = setInterval( function(){
                right.click();
            },2000);
    </script>
</body>
</html>